import requests
from openpyxl.reader.excel import load_workbook
import os




source_excel = load_workbook(r'D:\Desktop\word\file\file.xlsx')
source_sheet = source_excel['Sheet1']
datas = list()
a ="1***1"

print(a.replace("*",""))

for row in source_sheet.iter_rows(min_row=1):
    data = dict()
    data["dir3"] = str.strip(row[0].value.replace("*","").replace(r'"',""))
    data["dir1"] = str.strip(row[1].value)
    data["dir2"] = str.strip(row[2].value)
    data["kjxy"] = eval(row[3].value)
    data["shba"] = eval(row[4].value)
    data["tdsx"] = eval(row[5].value)
    data["sgsx"] = eval(row[6].value)
    datas.append(data)


for data in datas:
    dir_name = fr''
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)


for data in datas:
    kjxy = data['kjxy']
    if len(kjxy)>0:
        dir4= '框架协议'
        dir_name = fr''
        if not os.path.exists(dir_name):
            os.makedirs(dir_name)
        for myfile in kjxy:
            print(myfile)
            res = requests.get(f"{myfile['url']}")
            with open(fr'', 'wb') as f:
                f.write(res.content)
                print('写入完成')

    shba = data['shba']
    if len(shba) > 0:
        dir4 = '审核备案'
        dir_name = fr''
        if not os.path.exists(dir_name):
            os.makedirs(dir_name)
        for myfile in shba:
            print(myfile)
            res = requests.get(f"{myfile['url']}")
            with open(fr'D:\Desktop\word\file\2025\{data["dir1"]}\{data["dir2"]}\{data["dir3"]}\{dir4}\{myfile["name"]}','wb') as f:
                f.write(res.content)
                print('写入完成')

    tdsx = data['tdsx']
    if len(tdsx) > 0:
        dir4 = '土地手续'
        dir_name = fr''
        if not os.path.exists(dir_name):
            os.makedirs(dir_name)
        for myfile in tdsx:
            print(myfile)
            res = requests.get(f"")
            with open(fr'','wb') as f:
                    f.write(res.content)
                    print('写入完成')

    sgsx = data['sgsx']
    if len(sgsx) > 0:
        dir4 = '施工手续'
        dir_name = fr''
        if not os.path.exists(dir_name):
            os.makedirs(dir_name)
        for myfile in sgsx:
            print(myfile)
            res = requests.get(f"")
            with open(fr'','wb') as f:
                    f.write(res.content)
                    print('写入完成')
#
# response = requests.get("http://59.215.230.146:82/gate/webService/upload/download?fileName=df10a716ee314952850cec8c6dd2ed40.pdf&originalName=教体局办理马蹄小学工程规划许可证.pdf")
#
# with open(fr'D:\Desktop\word\file\all\遵义市\播州区\教体局办理马蹄小学工程规划许可证.pdf', 'wb') as f:
#     print(len(response.content))
#     print('开始写入')
#     f.write(response.content)
#     print('写入完成')

